package com.ruoyi.kong.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.kong.entity.KongEntity;
import com.ruoyi.kong.entity.KongRoute;
import com.ruoyi.kong.entity.KongRouteVo;
import com.ruoyi.kong.service.IKongRouteInfoService;
import com.ruoyi.kong.service.IKongServiceInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * kong 路由 控制类
 */
@RestController
@RequestMapping("/kong/routes")
public class KongRoutesController extends BaseController {


    @Autowired
    private IKongRouteInfoService kongRouteInfoService;


    /**
     * 返回 所有kong 服务信息
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo getKongRoutesInfos(){
        startPage();
        KongEntity<KongRoute> kongRouteList = kongRouteInfoService.getRouteList();
        return getDataTable(kongRouteList.getData());
    }


    /**
     * 路由新增
     * @param kongRoute
     * @return
     */
    @PostMapping("/add")
    public AjaxResult addRoute(@Validated @RequestBody KongRoute kongRoute){
        KongRoute _kongRoute = kongRouteInfoService.addRoute(kongRoute);
        return AjaxResult.success(_kongRoute);
    }


    /**
     * 删除 路由
     * @param routeId
     * @return
     */
    @DeleteMapping("/{routeId}")
    public AjaxResult deleteRoute(@PathVariable String routeId){
        kongRouteInfoService.deleteRoute(routeId);
        return success();
    }


    /**
     * 服务di 返回 路由信息
     * @param serviceId
     * @return
     */
    @GetMapping("/service/{serviceId}")
    public AjaxResult getRouteByServiceId(@PathVariable  String serviceId){
        KongEntity<KongRoute> kongRoute = kongRouteInfoService.routeByServiceId(serviceId);
        return AjaxResult.success(kongRoute.getData());
    }


    /**
     * 更新路由
     * @param id
     * @param kongRoute
     * @return
     */
    @PostMapping("/update/{id}")
    public AjaxResult updateRoute(@PathVariable String id,@RequestBody KongRouteVo kongRoute){
        KongRoute _kongRoute = kongRouteInfoService.updateRoute(id, kongRoute);
        return AjaxResult.success(_kongRoute);
    }

}
